import axios from "axios";

// 通用请求拦截器 - 添加Authorization头
const addAuthHeader = (config:any) => {
  const token = localStorage.getItem('Token');
  if (token) {
    config.headers.Authorization = `Bearer ${token}`;
  }
  return config;
};

// 通用响应拦截器 - 处理错误状态码
const handleResponseError = (error:any) => {
  if (error.response) {
    // 根据不同的HTTP状态码进行处理
    switch (error.response.status) {
      case 401: // 未授权
        // 清除本地存储的token
        localStorage.removeItem('Token');
        // 跳转到登录页
        window.location.href = '/login';
        break;
      case 403: // 禁止访问
        console.error('权限不足');
        break;
      case 500: // 服务器错误
        console.error('服务器内部错误');
        break;
    }
  }
  return Promise.reject(error);
};

// 实例化封装对象
const readB = axios.create({
  baseURL: "http://47.96.9.93:8000/dbdata/",
  timeout: 5000
});

const source = axios.create({
  baseURL: "http://localhost:5084",
  timeout: 5000
});

const fileUpload = axios.create({
  baseURL: "http://localhost:5089",
  timeout: 5000
});

// 吴维昊
const statementRed = axios.create({
/*   baseURL: "http://47.96.9.93:8000/", */
  baseURL: "http://47.96.9.93:8000/mentred/",
  timeout: 5000
});

const statementWrite = axios.create({
  baseURL: "http://localhost:5226",
  timeout: 5000
});

// 王雨龙
const BusinessRed = axios.create({
  baseURL: "http://47.96.9.93:8000/api/",
  timeout: 5000
});

const BusinessWrite = axios.create({
  baseURL: "http://47.96.9.93:8000/businesswrite/",
  timeout: 5000
});

// 张可欣
const FinanceRed = axios.create({
  baseURL: "http://47.96.9.93:8000/financeread",
  timeout: 5000
});

const FinanceWrite = axios.create({
  baseURL: "http://47.96.9.93:8000/financewrite",
  timeout: 5000
});

// 赵姝雅
const StorageRed = axios.create({
  baseURL: "http://47.96.9.93:8000/storageread",
  timeout: 5000
});

const StorageWrite = axios.create({
  baseURL: "http://47.96.9.93:8000/storagewrite",
  timeout: 5000
});

// 段栋兴
const BasicsRed = axios.create({
  baseURL: "http://47.96.9.93:8000/basicsread",
  timeout: 5000
});

const BasicsWrite = axios.create({
  baseURL: "http://47.96.9.93:8000/basicswrite",
  timeout: 5000
});

// 为需要认证的实例添加请求拦截器
readB.interceptors.request.use(addAuthHeader, error => Promise.reject(error));
statementRed.interceptors.request.use(addAuthHeader, error => Promise.reject(error));
statementWrite.interceptors.request.use(addAuthHeader, error => Promise.reject(error));
BusinessRed.interceptors.request.use(addAuthHeader, error => Promise.reject(error));
BusinessWrite.interceptors.request.use(addAuthHeader, error => Promise.reject(error));
FinanceRed.interceptors.request.use(addAuthHeader, error => Promise.reject(error));
FinanceWrite.interceptors.request.use(addAuthHeader, error => Promise.reject(error));
StorageRed.interceptors.request.use(addAuthHeader, error => Promise.reject(error));
StorageWrite.interceptors.request.use(addAuthHeader, error => Promise.reject(error));
BasicsRed.interceptors.request.use(addAuthHeader, error => Promise.reject(error));
BasicsWrite.interceptors.request.use(addAuthHeader, error => Promise.reject(error));

// 为需要错误处理的实例添加响应拦截器
readB.interceptors.response.use(res => res, handleResponseError);
statementRed.interceptors.response.use(res => res, handleResponseError);

// 导出封装对象
export {
  readB,
  source,
  fileUpload,
  statementRed,
  statementWrite,
  BusinessRed,
  BusinessWrite,
  FinanceRed,
  FinanceWrite,
  StorageRed,
  StorageWrite,
  BasicsRed,
  BasicsWrite
};